<?php

/**
* Tìm kiếm
*/
class Admin_SearchController extends Zend_Controller_Action{
	function init(){
		$this->params = $this->getRequest()->getParams();
		parent::init();
	}
	function indexAction(){
        // khai báo
        $Admin_Model_Search = new Admin_Model_Search();
        System_Template::using('blank','Admin');
        $step = 0;
        if(isset($this->params['step'])){
        $step = (int)$this->params['step'];
        }
        //tìm kiếm
        if($step == 0)
        {
            $step = "index";
            $rs = $Admin_Model_Search->count($this->params);
            $result = split("-",$rs);
            $count = $result[0];
            if($count > 0){
            $sql = $result[1];
            $paging = common::paging($count,'left','adminSearchIndex','hard_content');         
            $data = $Admin_Model_Search->search($sql, $paging[0], $paging[1]);
            }            
            $this->view->search = $this->params['searchContent'] ;
            $this->view->data = $data ;
            $this->view->paging = $paging[2] ;
            common::showButton('back');           
        }
        //hiển thị bảng lựa chọn sách
        else if($step == 1)
        {
           $step = "selectBook";
           $rs = $Admin_Model_Search->selectBook($this->params['ma'],$this->params['loai']);
          
           $this->view->data = $rs ;
           $ten_loai = array(1=>'sách',2=>'tài liệu',3=>'tạp chí');
           $this->view->ten_loai = $ten_loai[$this->params['loai']];
           $this->view->loai = (int)$this->params['loai'] ;
           $this->view->id = $this->params['ma'];
           $this->view->soluong = (int)$this->params['soluong'] ;
        }
        
        $this->renderScript("search/".$step.".phtml");
	}
    
    function detailAction(){
	$model = new Admin_Model_Search();
        $data = $model->getDetailById($this->params['id'],$this->params['loai']);
        $loai = $this->params['loai'];
        if($loai == 1){
            $name = "sách";
            $code = "MASACH";
            $namex = "TENSACH";
        }
        else if($loai == 2){
            $name = "tài liệu";
            $code = "MATAILIEU";
            $namex = "TENTAILIEU";
        }
        else if($loai == 3){
            $name = "tạp chí";
            $code = "MATAPCHI";
            $namex = "TENTAPCHI";
        }
        else {
            $name = "sách";
            $code = "MASACH";
            $namex = "TENSACH";
        }
        $this->view->data = $data ;
        $this->view->name = $name ;
        $this->view->namex = $namex ;
        $this->view->code = $code ;
	}
}